' 
' Copyright (C) 2014 Infinity CCS Ltd (http://www.infinityccs.com/)
' Copyright (C) 2007 Sasa Coh <sasacoh@gmail.com>
'
' This program is free software; you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation; either version 3 of the License, or
' (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with this program.  If not, see <http://www.gnu.org/licenses/>. 
'/

Imports Infinity.pjsipNET.Common
Imports Infinity.pjsipNET.Common.CallState
Imports Infinity.pjsipNET.Common.Config

Namespace CallControl.CallState

#Region "Idle"
  ''' <summary>
  ''' State Idle indicates the call is inactive
  ''' </summary>
  Friend Class Idle
    Inherits IState

    Public Sub New(sm As IStateMachine)
      MyBase.New(sm)
      Id = EStateId.IDLE
    End Sub

    Friend Overrides Sub OnEntry()

    End Sub

    Friend Overrides Sub OnExit()

    End Sub

    Public Overrides Function endCall() As Boolean
      _smref.Destroy()
      CallProxy.endCall()
      Return MyBase.endCall()
    End Function

    ''' <summary>
    ''' Make call to a given number and accountId. Assign sessionId to call state machine got from VoIP part.
    ''' </summary>
    ''' <param name="dialedNo"></param>
    ''' <returns></returns>
    Public Overrides Function makeCall(dialedNo As String, accountKey As String) As Integer
      _smref.CallingNumber = dialedNo
      '// make call and save sessionId
      _smref.ChangeState(EStateId.CONNECTING)
      _smref.SessionId = CallProxy.makeCall(dialedNo, accountKey)
      Return _smref.SessionId
    End Function

    ''' <summary>
    ''' Handle incoming call requests. Check for supplementary services flags.
    ''' </summary>
    ''' <param name="cli">CLI of the caller</param>
    ''' <param name="ddi">DDI of the call</param>
    ''' <param name="clidisplay">Name of the caller</param>
    Public Overrides Sub incomingCall(cli As String, ddi As String, clidisplay As String)
      Dim objAccount As IAccount = Nothing
      If _smref.AccountId >= 0 Then
        objAccount = _smref.Config.Account(_smref.AccountId)
      End If
      '// check supplementary services flags
      If (objAccount.CFUnconditional AndAlso objAccount.CFUnconditionalNumber.Length > 0) Then
        '// do NOT notify about state changes 
        _smref.DisableStateNotifications = True
        CallProxy.serviceRequest(CInt(EServiceCodes.SC_CFU), objAccount.CFUnconditionalNumber)
        Me.endCall()
        Exit Sub
      ElseIf (_smref.Config.DNDFlag = True) Then
        If (objAccount.CFBusy AndAlso objAccount.CFBusyNumber.Length > 0) Then
          '// do NOT notify about state changes 
          _smref.DisableStateNotifications = True
          CallProxy.serviceRequest(CInt(EServiceCodes.SC_CFB), objAccount.CFBusyNumber)
          Me.endCall()
          Exit Sub
        Else '// do NOT notify about state changes 
          _smref.DisableStateNotifications = True
          CallProxy.serviceRequest(CInt(EServiceCodes.SC_DND), "")
          Me.endCall()
          Exit Sub
        End If
      End If

      _smref.CallingNumber = cli
      _smref.CallingName = clidisplay
      _smref.DDI = ddi
      _smref.ChangeState(EStateId.INCOMING)
    End Sub

  End Class
#End Region

End Namespace
